package com.idbk.chargestation.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.idbk.chargestation.bean.JsonMyMessage;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息模块 数据库接口
 * @author lupc
 *
 */
public class MessageDatabase {

	private final DBHelper dbHelper;

	public MessageDatabase(Context context) {
		dbHelper = new DBHelper(context);
	}

	/**
	 * 插入消息
	 * @param message 信息
	 */
	public void insertMessage(JsonMyMessage message) {
		final String sql = "insert into " + DBHelper.TABLE_NAME_MESSAGE
				+ " (username,dtime,type,isRead,message)"
				+ " values(?,?,?,?,?);";
		SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
		sqlite.execSQL(
				sql,
				new String[]{
						message.username,
						message.date,
						message.messageType + "",
						message.isRead ? (1 + ""):(0 + ""),
						message.content
				});
		sqlite.close();
	}

	/**
	 * 删除全部消息
	 * @param username 用户名
	 */
	public void deleteAll(String username) {
		final String sql = "delete from " + DBHelper.TABLE_NAME_MESSAGE + " where username=?;";
		SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
		sqlite.execSQL(
				sql,
				new String[]{username});
		sqlite.close();
	}

	/**
	 * 删除单条消息
	 * @param id id
	 */
	public void deleteMessage(int id) {
		final String sql = "delete from " + DBHelper.TABLE_NAME_MESSAGE + " where id=?";
		SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
		sqlite.execSQL(
				sql,
				new String[]{id + ""});
		sqlite.close();
	}

	/**
	 * 设置 消息已读
	 * @param id id
	 */
	public void setMessageReaded(int id,boolean isRead) {
		final String sql = "update " + DBHelper.TABLE_NAME_MESSAGE + " set isRead=? where id=?";
		SQLiteDatabase sqlite = dbHelper.getWritableDatabase();
		sqlite.execSQL(
				sql,
				new String[]{ 
						isRead? (1+""):(0+""),
								id + ""});
		sqlite.close();
	}

	/**
	 * 根据 是否已读 为条件 读取本地消息列表
	 * @param isRead 是否已读
	 */
	public List<JsonMyMessage> selectMessage(String username, boolean isRead) {
		List<JsonMyMessage> data =  new ArrayList<JsonMyMessage>();
		SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
		Cursor cursor = sqlite.rawQuery(
				"select * from " + DBHelper.TABLE_NAME_MESSAGE + " where username=? and isRead=? order by id desc;",
				new String[]{
						username,
						isRead? (1+""):(0+"")});
		fillData(data,cursor);
		sqlite.close();	

		return data;
	}

	public List<JsonMyMessage> selectMessage(String username) {
		List<JsonMyMessage> data =  new ArrayList<JsonMyMessage>();
		SQLiteDatabase sqlite = dbHelper.getReadableDatabase();
		Cursor cursor = sqlite.rawQuery(
				"select * from " + DBHelper.TABLE_NAME_MESSAGE + " where username=? order by id desc;",
				new String[]{username});
		fillData(data,cursor);		
		sqlite.close();	

		return data;
	}

	private void fillData(List<JsonMyMessage> data, Cursor cursor) {
		for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
			JsonMyMessage message = new JsonMyMessage();
			message.id = cursor.getInt(0);
			message.username = cursor.getString(1);
			message.date = cursor.getString(2);			
			message.messageType = cursor.getInt(3);
			message.isRead = cursor.getInt(4) == 0 ? false : true;
			message.content = cursor.getString(5);
			data.add(message);
		}
		if(!cursor.isClosed()) {
			cursor.close();
		}
	}

	public void close() {
		dbHelper.close();
	}

}
